package com.zyz.bookborrowing.dao;

import com.zyz.bookborrowing.bean.User;
import com.zyz.bookborrowing.utils.DelFlagE;
import com.zyz.bookborrowing.utils.MyDButils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements IUserDao{
    @Override
    public List<User> list(User user) {
        QueryRunner queryRunner = MyDButils.getQueryRunner();
        String sql = "select * from t_user where is_deleted = ?";

        List<User> list = null;
        try {
//            BeanListHandler : 如果数据库表中的数据类型和User属性不一致，又无法自动转换，只能null
//            list = queryRunner.query(sql, new BeanListHandler<User>(User.class));
//            选择重写，对结果集ResultSet做处理，自行转换
            list = queryRunner.query(sql, new ResultSetHandler<List<User>>() {
                @Override
                public List<User> handle(ResultSet resultSet) throws SQLException {
                    List<User> list1 = new ArrayList<>();
                    while (resultSet.next()){
                        User user1 = new User();
                        user1.setId(resultSet.getInt("id"));
                        user1.setUsername(resultSet.getString("user_name"));
                        user1.setPassword(resultSet.getString("password"));
                        user1.setEmail(resultSet.getString("email"));
                        user1.setPhoneNum(resultSet.getString("phone_num"));
                        user1.setSalt(resultSet.getString("salt"));
                        user1.setIsDeleted(resultSet.getInt("is_deleted"));
                        list1.add(user1);
                    }
                    return list1;
                }
//                只找没有被删除的信息
            }, DelFlagE.NO.code);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Integer save(User user) {

        QueryRunner queryRunner = MyDButils.getQueryRunner();
        String sql = "insert into t_user(user_name, password, phone_num, email)values(?,?,?,?)";
        try {
//            返回的是影响更新的行数
            return queryRunner.update(sql, user.getUsername(), user.getPassword(), user.getPhoneNum(), user.getEmail());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Integer deleteById(Integer id) {
        QueryRunner queryRunner = MyDButils.getQueryRunner();
//        物理真正的删除
//        String sql = "delete from t_user where id = ?";
//        逻辑删除，仍能查询到
        String sql = "update t_user set is_deleted = ? where id = ?";
        try {
            return queryRunner.update(sql, DelFlagE.YES.code, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public User queryById(Integer id) {
        QueryRunner queryRunner = MyDButils.getQueryRunner();
        String sql = "select * from t_user where id = ? and is_deleted = ?";
        try {
            return queryRunner.query(sql, new ResultSetHandler<User>() {
                @Override
                public User handle(ResultSet rs) throws SQLException {
                    if (rs.next()) {
                        User user = new User();
                        user.setUsername(rs.getString("user_name"));
                        user.setId(id);
                        user.setPassword(rs.getString("password"));
                        user.setPhoneNum(rs.getString("phone_num"));
                        user.setEmail(rs.getString("email"));
                        return user;
                    }
                    return null;
                }
            }, id, DelFlagE.NO.code);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer updateUser(User user) {
        QueryRunner queryRunner = MyDButils.getQueryRunner();
        String sql = "update t_user set user_name = ?, password = ?, phone_num = ?, email = ? where id = ? ";
        try {
            return queryRunner.update(sql, user.getUsername(), user.getPassword(), user.getPhoneNum(), user.getEmail(), user.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return -1;
    }

    @Override
    public String checkUserName(String userName) {
        QueryRunner queryRunner = MyDButils.getQueryRunner();
        String sql = "select count(1) from t_user where is_deleted = ? and user_name = ?";
        try {
            int count = queryRunner.query(sql, new ResultSetHandler<Integer>() {
                @Override
                public Integer handle(ResultSet rs) throws SQLException {
                    rs.next();
//                    源码是显示从1开始
                    int count = rs.getInt(1);
                    return count;
                }
            }, DelFlagE.NO.code, userName);
            return count == 0 ? "success" : "error";
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "error";
    }

    @Override
    public User checkUserNP(String userName, String password) {
        QueryRunner queryRunner = MyDButils.getQueryRunner();
        String sql = "select * from t_user where is_deleted = ? and user_name = ? and password = ?";
        try {
            return queryRunner.query(sql, new ResultSetHandler<User>() {
                @Override
                public User handle(ResultSet rs) throws SQLException {
                    if (rs.next()) {
                        User user = new User();
                        user.setUsername(userName);
                        user.setId(rs.getInt("id"));
                        user.setPassword(password);
                        user.setPhoneNum(rs.getString("phone_num"));
                        user.setEmail(rs.getString("email"));
                        return user;

                    }
                    return null;
                }
            }, DelFlagE.NO.code, userName, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

//    public static void main(String[] args) {
//        UserDaoImpl dao = new UserDaoImpl();
//        for (User user : dao.list(null)) {
//            System.out.println(user);
//        }
//    }
}
